﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Timers;
using MoonLight.LibCommon;
using PVAutoTest.LibCommon;
using PVAutoTest.BizSimulator;

namespace PVAutoTest.TestMngr
{
    public class TestSuit : Notifier
    {
        public TestSuit()
        {
            m_tc_list = new List<TestCase>();
            m_suit_path = "";
            m_timer = new Timer();
            m_timer.Elapsed += new ElapsedEventHandler(timer_handler);
        }

        ~TestSuit()
        { 
        }

        public void SetSuitPath(string path, string data_path, string result_path)
        {
            m_suit_path = path;
            m_data_path = data_path;
            m_result_path = result_path;
        }

        public void SetTimeStep(int time_step)
        {
            m_timer.Interval = time_step;
        }

        public string GetSuitPath()
        {
            return m_suit_path;
        }

        public string GetSuitDataPath()
        {
            return m_data_path;
        }

        public string GetSuitResultPath()
        {
            return m_result_path;
        }

        public List<TestCase> GetTestCaseList(){
            return m_tc_list;
        }

        public int GetTestCaseCount() {
            return m_tc_list.Count;
        }

        public int AddTestCase(TestCase tc) {
            m_tc_list.Add(tc);
            return 0;
        }

        public void RunSuit()
        {
            m_cur_case_idx = 0;
            if (m_tc_list == null || m_tc_list.Count == 0) {
                Logger.WriteLog("no test case to run");
                return;
            }
            for (int i = 0; i < m_tc_list.Count; i++) 
            {
                //find the first activated case to run
                if (m_tc_list[i].IsEnable())
                {
                    m_cur_case_idx = i;
                    m_tc_list[i].RunCase();
                    break;
                }
            }
        }

        public override void handle_message(MessageInfo msg)
        {
            switch ((ProjectDefine.MsgType_e)msg.msg_type)
            {
                case ProjectDefine.MsgType_e.MSG_CASE_FINISHED:
                    {
                        m_timer.Start();
                    }
                    break;
                default:
                    Logger.WriteLog("unknown msg type [" + msg.msg_type.ToString() + "]");
                    break;
            }
        }

        private void timer_handler(object sender, ElapsedEventArgs e)
        {
            m_timer.Stop();
            for (int i = m_cur_case_idx + 1; i < m_tc_list.Count; i++)
            {
                //find the next activated case to run
                if (m_tc_list[i].IsEnable())
                {
                    m_cur_case_idx = i;
                    m_tc_list[i].RunCase();
                    break;
                }
            }
            return;
        }

        private List<TestCase> m_tc_list;
        private string m_suit_path;
        private string m_data_path;
        private string m_result_path;
        private int m_cur_case_idx;
        private Timer m_timer;
    }
}
